extends layout
block content
  +header
  .container
    ul.posts.spost
      li
        .author
          span(title=posts.name): a(href="/posts?author=" + posts.name) author: #{posts.name}
          span 评论数：#{posts.comments}
          span 浏览量：#{posts.pv}
        .comment_pv: span #{posts.moment}
        a(href="/posts/" + posts.id)
          .title
            img.userAvator(src="../images/" + posts.avator + ".png")
            | #{posts.title}
          .content.markdown!= posts.content
        .edit
          if session && session.user === posts.name
            p: a(href=posts['id'] + "/edit") 编辑
            p: a.delete_post 删除
  .comment_wrap
    if session.user
      form(class="form" method="post" action="/" + posts.id)
        textarea(id="spContent" name="content" cols="82")
        .submit 发表留言
    else
      p.tips 登录之后才可以评论哟
    if commentPageLength > 0
      .comment_list.markdown
        each res in pageOne
          .cmt_lists
            .cmt_content
              .userMsg
                img(src="../images/" + res['avator'] + ".png")
                span #{res['name']}
              .cmt_detail!= res['content']
              .cmt_time #{res['moment']}
              .cmt_name
                if session && session.user === res['name']
                  a.delete_comment(href="javascript:delete_comment(" + res['id'] + ");") 删除
    else
      p.tips 还没有评论，赶快去评论吧！
    .pagination(style="margin-top: 30px" id="page")
append script
  script(src="/pagination.js")
  script.
    var userName = "#{session.user}";
    pagination({
      selector: '#page',
      totalPage: #{commentPageLength},
      currentPage: 1,
      prev: '上一页',
      next: '下一页',
      first: true,
      last: true,
      showTotalPage: true,
      count: 2//当前页前面显示的数量
    }, function (val) {
      // 当前页
      var _comment = ''
      $.ajax({
        url: "#{posts.id}/commentPage",
        type: 'POST',
        data: {
          page: val
        },
        cache: false,
        success: function (msg) {
          //console.log(msg)
          _comment = ''
          if (msg != 'error') {
            $('.comment_list').html(' ')
            $.each(msg, function (i, val) {
              //console.log(val.content)
              _comment += '<div class=\"cmt_lists\"><div class=\"cmt_content\"><div class=\"userMsg\"><img src = \"../images/' + val.avator + '.png\" ><span>' + val.name + '</span></div ><div class="cmt_detail">' + val.content + '</div><span class=\"cmt_time\">' + val.moment + '</span><span class=\"cmt_name\">';
              if (val.name == userName) {
                _comment += '<a class=\"delete_comment\" href=\"javascript:delete_comment(' + val.id + ');\"> 删除</a>'
              }
              _comment += '</span></div></div>'
            })
            $('.comment_list').append(_comment)
          } else {
            alert('分页不存在')
          }
        }
      })
    })
    // 删除文章
    $('.delete_post').click(function () {
      $.ajax({
        url: "#{posts.id}/remove",
        type: 'POST',
        cache: false,
        success: function (msg) {
          if (msg.code == 200) {
            fade('删除文章成功')
            setTimeout(function () {
              window.location.href = "/posts"
            }, 1000)
          } else {
            fade('删除文章失败')
          }
        }
      })
    })
    // 评论
    var isAllow = true
    $('.submit').click(function(){
      console.log(123123)
      if (!isAllow) return
      isAllow = false
      if ($('textarea').val().trim() == '') {
        fade('请输入评论！')
        isAllow = true
      } else {
        $.ajax({
          url: '/' + location.pathname.split('/')[2],
          data: $('.form').serialize(),
          type: "POST",
          cache: false,
          dataType: 'json',
          success: function (msg) {
            if (msg.code == 200) {
              fade('发表留言成功');
              setTimeout(function () {
                isAllow = true
                window.location.reload()
              }, 1500)
            }
          },
          error: function () {
            alert('异常');
          }
        })
      }
    })
    // 删除评论
    function delete_comment(id) {
      $.ajax({
        url: "#{posts.id}/comment/" + id + "/remove",
        type: 'POST',
        cache: false,
        success: function (msg) {
          if (msg.code == 200) {
            fade('删除留言成功')
            setTimeout(function () {
              location.reload()
            }, 1000)
          } else {
            fade('删除留言失败');
          }
        },
        error: function () {
          alert('异常')
        }
      })
    }